package com.jinlejia.erp.ui.customer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.FilterArea1Adapter;
import com.jinlejia.erp.adapter.FilterArea2Adapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityLocationBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 位置
 */
public class LocationActivity extends AppCompatActivity {

    //片区
    FilterArea1Adapter area1Adapter;
    ArrayList<Map<String,Object>> areaData = new ArrayList<>();
    FilterArea2Adapter area2Adapter;
    ArrayList<String> area2Data = new ArrayList<>();
    private String countyName,districtname;

    private ActivityLocationBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_location);
        binding.btBack.setOnClickListener(this::onClick);
        binding.btSave.setOnClickListener(this::onClick);

        binding.countyRecycler.setLayoutManager(new LinearLayoutManager(this));
        binding.countyRecycler.setHasFixedSize(true);
        binding.districtRecycler.setLayoutManager(new LinearLayoutManager(this));
        binding.districtRecycler.setHasFixedSize(true);

        area1Adapter = new FilterArea1Adapter(areaData);
        area2Adapter = new FilterArea2Adapter(area2Data);
        binding.countyRecycler.setAdapter(area1Adapter);
        binding.districtRecycler.setAdapter(area2Adapter);

        if( getIntent().hasExtra("county") ){
            area1Adapter.setCounty(getIntent().getStringExtra("county"));
        }
        if( getIntent().hasExtra("area") ){
            area2Adapter.setArea(getIntent().getStringExtra("area"));
        }

        area1Adapter.setOnItemSelectedListener(map -> {
            area2Data.clear();
            if( map.get("countyName").toString().equals("不限") ){
                countyName = "";
                districtname="";
            }else{
                area2Data.addAll(JSON.parseArray(map.get("areaList").toString(),String.class));
                if( !getIntent().hasExtra("no_limit") ) {
                    area2Data.add(0, "不限");
                }
                countyName = map.get("countyName").toString();
            }
            area2Adapter.cur_position = -1;
            area2Adapter.setData(area2Data);

        });
        area1Adapter.setOnItemRemoveListener(map -> {

        });
        area2Adapter.setOnItemSelectedListener(s -> {
            if( s.equals("不限") ){
             districtname="";
            }else {
                districtname = s;
            }
        });
        area2Adapter.setOnItemRemoveListener(s -> {
            districtname = "";
        });

        getData();
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                Intent d = new Intent();
                setResult(RESULT_CANCELED,d);
                finish();
                break;
            case R.id.bt_save:
                    Intent data = new Intent();
                    data.putExtra("countyName", countyName);
                    data.putExtra("districtname", districtname);
                    setResult(RESULT_OK, data);
                    finish();
                break;
            default:
                break;
        }
    }

    private void getData() {

        OkHttpUtils.get()
                .url(ApiConfig.COUNTY_AREA)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(LocationActivity.this, response)) {
                            JSONArray data = JSON.parseArray(response.get("data").toString());
                            ArrayList<Map<String, Object>> areaData = JSON.parseObject(data.toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            if( !getIntent().hasExtra("no_limit") ) {
                                Map<String, Object> m = new HashMap<String, Object>();
                                m.put("countyName", "不限");
                                areaData.add(0, m);
                            }
                            area1Adapter.setData(areaData);
                        }
                    }
                });

    }
}
